#include <iostream>
#include <stack>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class BSTIterator {
private:
    stack<TreeNode*> node_stack;
public:
    BSTIterator(TreeNode* root) {
        while (root != nullptr) {
            node_stack.push(root);
            root = root->left;
        }
    }

    /** @return the next smallest number */
    int next() {
        TreeNode* top = node_stack.top();
        node_stack.pop();
        int next = top->val;
        top = top->right;
        while (top != nullptr) {
            node_stack.push(top);
            top = top->left;
        }
        return next;
    }

    /** @return whether we have a next smallest number */
    bool hasNext() {
        return node_stack.size() != 0;
    }
};

/**
 * Your BSTIterator object will be instantiated and called as such:
 * BSTIterator* obj = new BSTIterator(root);
 * int param_1 = obj->next();
 * bool param_2 = obj->hasNext();
 */

